package login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.People;

import com.google.gson.Gson;
public class helloWorld extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if(req!=null){
			System.out.println("收到前台传来的东西了！");
		}
	
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json;charset=utf8");	
		
		PrintWriter out=resp.getWriter();
		String param=req.getParameter("id");	
		System.out.println(param);
		if("1".equals(param)){
			Gson gson = new Gson();
			People p=new People(10,"zhangsan","nan");
			String str=gson.toJson(p);
			System.out.println(str);	
		 out.print(str);
		 }else{
			Gson gson = new Gson();
			People p=new People(12,"wangli","nv");
			String str=gson.toJson(p);
			out.print(str);
		}
		System.out.println("结束了");
 		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if(req!=null){
			System.out.println("收到前台传来的东西了！");
		}
		
		
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json;charset=UTF-8");
		PrintWriter out=resp.getWriter();
		String param=req.getParameter("id");	
		System.out.println(param);
		if("1".equals(param)){
			Gson gson = new Gson();
			People p1=new People(10,"zhangsan","nan");
			People p2=new People(15,"liwang","女");
			List<People> list=new ArrayList<People>();
			list.add(p1);
			list.add(p2);
			
			String str=gson.toJson(list);
			System.out.println(str);
		 out.print(str);
		 }else{
			Gson gson = new Gson();
			People p1=new People(12,"wangli","nv");
			People p2=new People(14,"lig","女");
			List<People> list=new ArrayList<People>();
			list.add(p1);
			list.add(p2);
			String str=gson.toJson(list);	
			out.print(str);
		}
		System.out.println("结束了");
	}
}
